﻿#region Using Directives
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Web.UI;
using System.Web.UI.Design;

using PAT.Entities;
using PAT.Data;
using PAT.Data.Bases;
using PAT.Services;
#endregion

namespace PAT.Web.Data
{
	/// <summary>
	/// Represents the DataRepository.PatUsuarioProvider object that provides
	/// data to data-bound controls in multi-tier Web application architectures.
	/// </summary>
	[Designer(typeof(PatUsuarioDataSourceDesigner))]
	public class PatUsuarioDataSource : ProviderDataSource<PatUsuario, PatUsuarioKey>
	{
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the PatUsuarioDataSource class.
		/// </summary>
		public PatUsuarioDataSource() : base(new PatUsuarioService())
		{
		}

		#endregion Constructors
		
		#region Properties
		
		/// <summary>
		/// Gets a reference to the PatUsuarioDataSourceView used by the PatUsuarioDataSource.
		/// </summary>
		protected PatUsuarioDataSourceView PatUsuarioView
		{
			get { return ( View as PatUsuarioDataSourceView ); }
		}
		
		/// <summary>
		/// Gets or sets the name of the method or function that
		/// the PatUsuarioDataSource control invokes to retrieve data.
		/// </summary>
		public PatUsuarioSelectMethod SelectMethod
		{
			get
			{
				PatUsuarioSelectMethod selectMethod = PatUsuarioSelectMethod.GetAll;
				Object method = ViewState["SelectMethod"];
				if ( method != null )
				{
					selectMethod = (PatUsuarioSelectMethod) method;
				}
				return selectMethod;
			}
			set { ViewState["SelectMethod"] = value; }
		}

		#endregion Properties
		
		#region Methods

		/// <summary>
		/// Creates a new instance of the PatUsuarioDataSourceView class that is to be
		/// used by the PatUsuarioDataSource.
		/// </summary>
		/// <returns>An instance of the PatUsuarioDataSourceView class.</returns>
		protected override BaseDataSourceView<PatUsuario, PatUsuarioKey> GetNewDataSourceView()
		{
			return new PatUsuarioDataSourceView(this, DefaultViewName);
		}
		
		/// <summary>
        /// Creates a cache hashing key based on the startIndex, pageSize and the SelectMethod being used.
        /// </summary>
        /// <param name="startIndex">The current start row index.</param>
        /// <param name="pageSize">The current page size.</param>
        /// <returns>A string that can be used as a key for caching purposes.</returns>
		protected override string CacheHashKey(int startIndex, int pageSize)
        {
			return String.Format("{0}:{1}:{2}", SelectMethod, startIndex, pageSize);
        }
		
		#endregion Methods
	}
	
	/// <summary>
	/// Supports the PatUsuarioDataSource control and provides an interface for
	/// data-bound controls to perform data operations with business and data objects.
	/// </summary>
	public class PatUsuarioDataSourceView : ProviderDataSourceView<PatUsuario, PatUsuarioKey>
	{
		#region Declarations

		#endregion Declarations
		
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the PatUsuarioDataSourceView class.
		/// </summary>
		/// <param name="owner">A reference to the PatUsuarioDataSource which created this instance.</param>
		/// <param name="viewName">The name of the view.</param>
		public PatUsuarioDataSourceView(PatUsuarioDataSource owner, String viewName)
			: base(owner, viewName)
		{
		}
		
		#endregion Constructors
		
		#region Properties

		/// <summary>
		/// Gets a strongly-typed reference to the Owner property.
		/// </summary>
		internal PatUsuarioDataSource PatUsuarioOwner
		{
			get { return Owner as PatUsuarioDataSource; }
		}

		/// <summary>
		/// Gets or sets the name of the method or function that
		/// the DataSource control invokes to retrieve data.
		/// </summary>
		internal PatUsuarioSelectMethod SelectMethod
		{
			get { return PatUsuarioOwner.SelectMethod; }
			set { PatUsuarioOwner.SelectMethod = value; }
		}

		/// <summary>
		/// Gets a strongly typed reference to the Provider property.
		/// </summary>
		internal PatUsuarioService PatUsuarioProvider
		{
			get { return Provider as PatUsuarioService; }
		}

		#endregion Properties
		
		#region Methods
		
		/// <summary>
		/// Gets a collection of Entity objects based on the value of the SelectMethod property.
		/// </summary>
		/// <param name="count">The total number of rows in the DataSource.</param>
		/// <returns>A collection of Entity objects.</returns>
		protected override IList<PatUsuario> GetSelectData(out int count)
		{
			Hashtable values = CollectionsUtil.CreateCaseInsensitiveHashtable(GetParameterValues());
			Hashtable customOutput = CollectionsUtil.CreateCaseInsensitiveHashtable();
			IList<PatUsuario> results = null;
			PatUsuario item;
			count = 0;
			
			System.Int32 _idUsuario;
			System.Int32 _idEmpresa;
			System.Int16 _idTipoIdentificacion;
			System.Int32 _idUbicacionCiudad;
			System.Int16 _idRol;

			switch ( SelectMethod )
			{
				case PatUsuarioSelectMethod.Get:
					PatUsuarioKey entityKey  = new PatUsuarioKey();
					entityKey.Load(values);
					item = PatUsuarioProvider.Get(entityKey);
					results = new TList<PatUsuario>();
					if ( item != null ) results.Add(item);
					count = results.Count;
					break;
				case PatUsuarioSelectMethod.GetAll:
                    results = PatUsuarioProvider.GetAll(StartIndex, PageSize, out count);
                    break;
				case PatUsuarioSelectMethod.GetPaged:
					results = PatUsuarioProvider.GetPaged(WhereClause, OrderBy, PageIndex, PageSize, out count);
					break;
				case PatUsuarioSelectMethod.Find:
					if ( FilterParameters != null )
						results = PatUsuarioProvider.Find(FilterParameters, OrderBy, StartIndex, PageSize, out count);
					else
						results = PatUsuarioProvider.Find(WhereClause, StartIndex, PageSize, out count);
                    break;
				// PK
				case PatUsuarioSelectMethod.GetByIdUsuario:
					_idUsuario = ( values["IdUsuario"] != null ) ? (System.Int32) EntityUtil.ChangeType(values["IdUsuario"], typeof(System.Int32)) : (int)0;
					item = PatUsuarioProvider.GetByIdUsuario(_idUsuario);
					results = new TList<PatUsuario>();
					if ( item != null ) results.Add(item);
					count = results.Count;
					break;
				// IX
				// FK
				case PatUsuarioSelectMethod.GetByIdEmpresa:
					_idEmpresa = ( values["IdEmpresa"] != null ) ? (System.Int32) EntityUtil.ChangeType(values["IdEmpresa"], typeof(System.Int32)) : (int)0;
					results = PatUsuarioProvider.GetByIdEmpresa(_idEmpresa, this.StartIndex, this.PageSize, out count);
					break;
				case PatUsuarioSelectMethod.GetByIdTipoIdentificacion:
					_idTipoIdentificacion = ( values["IdTipoIdentificacion"] != null ) ? (System.Int16) EntityUtil.ChangeType(values["IdTipoIdentificacion"], typeof(System.Int16)) : (short)0;
					results = PatUsuarioProvider.GetByIdTipoIdentificacion(_idTipoIdentificacion, this.StartIndex, this.PageSize, out count);
					break;
				case PatUsuarioSelectMethod.GetByIdUbicacionCiudad:
					_idUbicacionCiudad = ( values["IdUbicacionCiudad"] != null ) ? (System.Int32) EntityUtil.ChangeType(values["IdUbicacionCiudad"], typeof(System.Int32)) : (int)0;
					results = PatUsuarioProvider.GetByIdUbicacionCiudad(_idUbicacionCiudad, this.StartIndex, this.PageSize, out count);
					break;
				// M:M
				case PatUsuarioSelectMethod.GetByIdRolFromPatUsuarioRol:
					_idRol = ( values["IdRol"] != null ) ? (System.Int16) EntityUtil.ChangeType(values["IdRol"], typeof(System.Int16)) : (short)0;
					results = PatUsuarioProvider.GetByIdRolFromPatUsuarioRol(_idRol, this.StartIndex, this.PageSize, out count);
					break;
				// Custom
				default:
					break;
			}

			if ( results != null && count < 1 )
			{
				count = results.Count;

				if ( !String.IsNullOrEmpty(CustomMethodRecordCountParamName) )
				{
					object objCustomCount = EntityUtil.ChangeType(customOutput[CustomMethodRecordCountParamName], typeof(Int32));
					
					if ( objCustomCount != null )
					{
						count = (int) objCustomCount;
					}
				}
			}
			
			return results;
		}
		
		/// <summary>
		/// Gets the values of any supplied parameters for internal caching.
		/// </summary>
		/// <param name="values">An IDictionary object of name/value pairs.</param>
		protected override void GetSelectParameters(IDictionary values)
		{
			if ( SelectMethod == PatUsuarioSelectMethod.Get || SelectMethod == PatUsuarioSelectMethod.GetByIdUsuario )
			{
				EntityId = GetEntityKey(values);
			}
		}

		/// <summary>
		/// Performs a DeepLoad operation for the current entity if it has
		/// not already been performed.
		/// </summary>
		internal override void DeepLoad()
		{
			if ( !IsDeepLoaded )
			{
				PatUsuario entity = GetCurrentEntity();
				
				if ( entity != null )
				{
					// init transaction manager
					GetTransactionManager();
					// execute deep load method
					PatUsuarioProvider.DeepLoad(GetCurrentEntity(), EnableRecursiveDeepLoad);
					// set loaded flag
					IsDeepLoaded = true;
				}
			}
		}

		/// <summary>
		/// Performs a DeepLoad operation on the specified entity collection.
		/// </summary>
		/// <param name="entityList"></param>
		/// <param name="properties"></param>
		internal override void DeepLoad(TList<PatUsuario> entityList, ProviderDataSourceDeepLoadList properties)
		{
			// init transaction manager
			GetTransactionManager();
			// execute deep load method
			PatUsuarioProvider.DeepLoad(entityList, properties.Recursive, properties.Method, properties.GetTypes());
		}

		#endregion Select Methods
	}
	
	#region PatUsuarioDataSourceDesigner

	/// <summary>
	/// Provides design-time support in a design host for the PatUsuarioDataSource class.
	/// </summary>
	public class PatUsuarioDataSourceDesigner : ProviderDataSourceDesigner<PatUsuario, PatUsuarioKey>
	{
		/// <summary>
		/// Initializes a new instance of the PatUsuarioDataSourceDesigner class.
		/// </summary>
		public PatUsuarioDataSourceDesigner()
		{
		}

		/// <summary>
		/// Gets or sets the SelectMethod property.
		/// </summary>
		public PatUsuarioSelectMethod SelectMethod
		{
			get { return ((PatUsuarioDataSource) DataSource).SelectMethod; }
			set { SetPropertyValue("SelectMethod", value); }
		}

		/// <summary>Gets the designer action list collection for this designer.</summary>
		/// <returns>The <see cref="T:System.ComponentModel.Design.DesignerActionListCollection"/>
		/// associated with this designer.</returns>
		public override DesignerActionListCollection ActionLists
		{
			get
			{
				DesignerActionListCollection actions = new DesignerActionListCollection();
				actions.Add(new PatUsuarioDataSourceActionList(this));
				actions.AddRange(base.ActionLists);
				return actions;
			}
		}
	}

	#region PatUsuarioDataSourceActionList

	/// <summary>
	/// Supports the PatUsuarioDataSourceDesigner class.
	/// </summary>
	internal class PatUsuarioDataSourceActionList : DesignerActionList
	{
		private PatUsuarioDataSourceDesigner _designer;

		/// <summary>
		/// Initializes a new instance of the PatUsuarioDataSourceActionList class.
		/// </summary>
		/// <param name="designer"></param>
		public PatUsuarioDataSourceActionList(PatUsuarioDataSourceDesigner designer) : base(designer.Component)
		{
			_designer = designer;
		}

		/// <summary>
		/// Gets or sets the SelectMethod property.
		/// </summary>
		public PatUsuarioSelectMethod SelectMethod
		{
			get { return _designer.SelectMethod; }
			set { _designer.SelectMethod = value; }
		}

		/// <summary>
		/// Returns the collection of <see cref="T:System.ComponentModel.Design.DesignerActionItem"/>
		/// objects contained in the list.
		/// </summary>
		/// <returns>A <see cref="T:System.ComponentModel.Design.DesignerActionItem"/>
		/// array that contains the items in this list.</returns>
		public override DesignerActionItemCollection GetSortedActionItems()
		{
			DesignerActionItemCollection items = new DesignerActionItemCollection();
			items.Add(new DesignerActionPropertyItem("SelectMethod", "Select Method", "Methods"));
			return items;
		}
	}

	#endregion PatUsuarioDataSourceActionList
	
	#endregion PatUsuarioDataSourceDesigner
	
	#region PatUsuarioSelectMethod
	
	/// <summary>
	/// Enumeration of method names available for the PatUsuarioDataSource.SelectMethod property.
	/// </summary>
	public enum PatUsuarioSelectMethod
	{
		/// <summary>
		/// Represents the Get method.
		/// </summary>
		Get,
		/// <summary>
		/// Represents the GetAll method.
		/// </summary>
		GetAll,
		/// <summary>
		/// Represents the GetPaged method.
		/// </summary>
		GetPaged,
		/// <summary>
		/// Represents the Find method.
		/// </summary>
		Find,
		/// <summary>
		/// Represents the GetByIdUsuario method.
		/// </summary>
		GetByIdUsuario,
		/// <summary>
		/// Represents the GetByIdEmpresa method.
		/// </summary>
		GetByIdEmpresa,
		/// <summary>
		/// Represents the GetByIdTipoIdentificacion method.
		/// </summary>
		GetByIdTipoIdentificacion,
		/// <summary>
		/// Represents the GetByIdUbicacionCiudad method.
		/// </summary>
		GetByIdUbicacionCiudad,
		/// <summary>
		/// Represents the GetByIdRolFromPatUsuarioRol method.
		/// </summary>
		GetByIdRolFromPatUsuarioRol
	}
	
	#endregion PatUsuarioSelectMethod

	#region PatUsuarioFilter
	
	/// <summary>
	/// A strongly-typed instance of the <see cref="SqlFilter&lt;EntityColumn&gt;"/> class
	/// that is used exclusively with a <see cref="PatUsuario"/> object.
	/// </summary>
	[CLSCompliant(true)]
	public class PatUsuarioFilter : SqlFilter<PatUsuarioColumn>
	{
	}
	
	#endregion PatUsuarioFilter

	#region PatUsuarioExpressionBuilder
	
	/// <summary>
	/// A strongly-typed instance of the <see cref="SqlExpressionBuilder&lt;EntityColumn&gt;"/> class
	/// that is used exclusively with a <see cref="PatUsuario"/> object.
	/// </summary>
	[CLSCompliant(true)]
	public class PatUsuarioExpressionBuilder : SqlExpressionBuilder<PatUsuarioColumn>
	{
	}
	
	#endregion PatUsuarioExpressionBuilder	

	#region PatUsuarioProperty
	
	/// <summary>
	/// A strongly-typed instance of the <see cref="ChildEntityProperty&lt;PatUsuarioChildEntityTypes&gt;"/> class
	/// that is used exclusively with a <see cref="PatUsuario"/> object.
	/// </summary>
	[CLSCompliant(true)]
	public class PatUsuarioProperty : ChildEntityProperty<PatUsuarioChildEntityTypes>
	{
	}
	
	#endregion PatUsuarioProperty
}

